/*
 * Corey Birdsall, Gabriel Noblesmith
 * Senior Project Code
 * Oct. 5, 2011
 * This program will receive an incoming analog signal on the ADC.
 * The frequency of the signal will be determined.  Finally, a motor
 * will be controlled to adjust the incoming frequency to a desired
 * value.
 */

#define F_CPU 1000000
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdio.h>

int initialize(void);

int main(void) {
	initialize();

	while (1) {
	}

	return 0;
}


// Initialize ADC, timing, etc.
// returns 0 on success,
// returns -1 on failure
int initialize(void)
{
	// initialize ADC first

	// ~REFS1, REFS0 -> Use AREF for reference with external capacitor
	// ~ADLAR -> Right-adjusted output
	// ~ADMUX[4:0] -> Single-ended input on ADC0 (PA0) 
	ADMUX = REFS0;

	
	ADCSRA = 0;

	// ~ADTS2. ADTS1. ADTS0 -> Use Timer0 Compare Match Interrupt
	ADCSRB = ADTS1 | ADTS0;

	// enable ADC (use ADATE to enable auto-triggering)
	ADCSRA = ADEN;

	// initialize timer
	
	// ~WGM02, WGM01, ~WGM00 -> CTC, TOP = OCRA
	// NOTE: WGM02 is actually in TCCR0B
	TCCR0A = WGM01;
	// CS02, ~CS01, ~CS00 -> use clk/256
	TCCR0B = CS02;

	// Set compare amount
	OCR0A = 200;

	// enable compare A interrupts
	TIMSK0 = OCIE0A;

	// set up i/o
	
	// PORTC all outputs
	DDRC = 0xFF;

	// global enable interrupts	
	SREG = 0x80;

	return 0;
}

ISR(TIMER0_COMPA)
{
PORTC = ~PORTC;

}

